<?php
require_once("lib/curl.php");
require_once("config.php");

Doctrine::loadModels('models');

//Reformat the headcount nonsense so that it's actually xml.
$fname = "tests/testraiddata2.xml";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = preg_replace("/key\d+/", "key", $content);
$fhandle = fopen($fname . ".fixed","w");
fwrite($fhandle,$content);
fclose($fhandle);

$xmlFileData = file_get_contents("tests/testraiddata2.xml.fixed");
$xmlData = new SimpleXMLElement($xmlFileData);
//And here's the output.

print_r($xmlData);

//This works.
$raid = new Raid();
$raid->startdate = $xmlData->start;
$raid->enddate = $xmlData->end;
$raid->zone = $xmlData->zone;
$raid->headcountfile = $fname;
$raid->save();

$raiderTable = Doctrine::getTable('Raider');

foreach ($xmlData->PlayerInfos->key as $player){
	//print("Name: " . $player->name . " Class: " . $player->class . "\r\n");
	$raider = $raiderTable->find($player->name);
	if($raider === null || !isset($raider->name)){
	        //print("not found\r\n");
		$raider = new Raider();
		$raider->name = $player->name;
		$raider->class = $player->class;
		$raider->save();
	} else {
		//print("FOUND... ");
		//print($raider .  "\r\n");
	}
	$att = new RaidRaider();
	$att->raid_id = $raid->id;
	$att->raider_id = $raider->name;
	$att->save();
}

//TODO Do attendance on the per-boss level.


print ("Instance: " . $xmlData->zone . "\r\n");
print ("Start Timestamp: " . $xmlData->start . "\r\n");
print ("End Timestamp: " . $xmlData->end . "\r\n");
//print ("Player Infos: " . $xmlData->PlayerInfos->key[5]->name . "\r\n");
?>
